package com.fyzk.activity.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.fyzk.activity.fragment.ScoreFragment;
import com.fyzk.base.BaseActivity;
import com.fyzk.base.BaseWebFragment;
import com.fyzk.view.TopTitleView;
import com.tyjc.lottery.R;

import org.xutils.view.annotation.ContentView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@ContentView(R.layout.real_time_score_activity)
public class RealtimeScoreActivity extends BaseActivity {
    private TextView zuqiu, lanqiu;
    private BaseWebFragment zuqiuFragment, lanqiuFragment;
    private int cur = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        zuqiu = findViewById(R.id.zuqiu);
        TopTitleView topView = (TopTitleView) findViewById(R.id.order_topview);
        topView.setOnTitleClickListener(this);
        lanqiu = findViewById(R.id.lanqiu);
        zuqiu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                zuqiu.setBackground(getDrawable(R.drawable.real_time_score_selected_bg));
                lanqiu.setBackground(null);
                selectFragment(0);
            }
        });
        lanqiu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lanqiu.setBackground(getDrawable(R.drawable.real_time_score_selected_bg));
                zuqiu.setBackground(null);
                selectFragment(1);
            }
        });

        selectFragment(0);
    }

    private void selectFragment(int i) {
        if(cur == i){
            return;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(calendar.DATE,-1);
        String date2= simpleDateFormat.format(calendar.getTime());
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ftran = fm.beginTransaction();
        switch (i) {
            case 0:
                cur = 0;
                zuqiuFragment = (BaseWebFragment) getSupportFragmentManager().findFragmentByTag("zuqiu");
                if (zuqiuFragment == null) {
                    zuqiuFragment = BaseWebFragment.newInstance("https://live.m.500.com/home/zq/jczq/cur?render=local",0);
                }
                if (!zuqiuFragment.isAdded()) {
                    ftran.add(R.id.fl_main_content, zuqiuFragment,"zuqiu");
                }
                if (lanqiuFragment != null)
                    ftran.hide(lanqiuFragment);
                ftran.show(zuqiuFragment);
                break;
            case 1:
                cur = 1;
                lanqiuFragment = (BaseWebFragment) getSupportFragmentManager().findFragmentByTag("lanqiu");
                if (lanqiuFragment == null) {
                    lanqiuFragment = BaseWebFragment.newInstance("https://live.m.500.com/home/lq/jclq/cur?render=local",1);
                }
                if (!lanqiuFragment.isAdded()) {
                    ftran.add(R.id.fl_main_content, lanqiuFragment,"lanqiu");
                }
                if (zuqiuFragment != null)
                    ftran.hide(zuqiuFragment);
                ftran.show(lanqiuFragment);
                break;

        }

        ftran.commit();
    }

    @Override
    public void onBackPressed() {
//        if(cur == 0){
//            if(zuqiuFragment.canBack()){
//                zuqiuFragment.back();
//            }else {
//                super.onBackPressed();
//            }
//        }else if(cur == 1){
//            if(lanqiuFragment.canBack()){
//                lanqiuFragment.back();
//            }else {
//                super.onBackPressed();
//            }
//        }else {
            super.onBackPressed();
//        }
    }
}
